/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
#include <stdio.h>
struct ListNode
{
     int val;
     struct ListNode *next;
};

struct ListNode* removeElements(struct ListNode* head, int val)
{
    typedef struct ListNode ListNode;
    ListNode* NewHead, *NewTail;
    NewHead = NewTail = NULL;
    ListNode* pcur = head;
    while(pcur)
    {
        if(pcur->val != val)
        {
            if(NewHead == NULL)
            {
                NewHead = NewTail = pcur;
            }
            else
            {
                NewTail->next = pcur;
                NewTail = NewTail->next;
            }
        }
        
        pcur = pcur->next;
    }
    if(NewTail)
    {
        NewTail->next = NULL;
    }
    return NewHead;
}
